package com.ruoyi.interfaceConfig.businessObject.service.impl;

import com.ruoyi.interfaceConfig.businessObject.domain.BusinessFieldSetting;
import com.ruoyi.interfaceConfig.businessObject.domain.vo.FieldSettingVo;
import com.ruoyi.interfaceConfig.businessObject.mapper.BusinessFieldSettingMapper;
import com.ruoyi.interfaceConfig.businessObject.service.IBusinessFieldSettingService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author YiYuan
 * @description
 * @date 2024/2/26
 */
@Service
@RequiredArgsConstructor
public class IBusinessFieldSettingServiceImpl implements IBusinessFieldSettingService {
    private final BusinessFieldSettingMapper businessFieldSettingMapper;
    @Override
    public List<BusinessFieldSetting> selectBusinessFieldSettingList(BusinessFieldSetting businessFieldSetting) {
        List<BusinessFieldSetting> businessFieldSettings = businessFieldSettingMapper.selectBusinessFieldSettingList(businessFieldSetting);
        List<Long> list= new  ArrayList<Long>();
        for (BusinessFieldSetting b:businessFieldSettings) {
            if (b.getIsForeignKey()==0){
                list.add(b.getAssociationTable());
            }
        }
            for (Long l:list) {
                businessFieldSetting.setBusinessId(l);
                List<BusinessFieldSetting> businessFieldSettings02 = businessFieldSettingMapper.selectBusinessFieldSettingList(businessFieldSetting);
                businessFieldSettings.addAll(businessFieldSettings02);
            }
        return businessFieldSettings;
    }

    @Override
    public FieldSettingVo selectFieldById(Long sceneId,Long sysObjId1,Long sysObjId2) {
        return businessFieldSettingMapper.selectFieldById(sceneId, sysObjId1, sysObjId2);
    }

    @Override
    public List<String> selectFieldListById(Long sceneId, Long sysObjId) {
        return businessFieldSettingMapper.selectFieldListById(sceneId, sysObjId);
    }

    @Override
    public List<String> selectPrimaryKeysById(Long sysObjId2) {
        return businessFieldSettingMapper.selectPrimaryKeysById(sysObjId2);
    }

    @Override
    public List<List<String>> selectFieldByColumnName(List<String> sysObj2Field) {
        List<List<String>> lists = new ArrayList<>();
        List<List<String>> lists01 = new ArrayList<>();
        for (String columnName:sysObj2Field) {
            List<String> list=businessFieldSettingMapper.selectBusinessFieldSettingBycolumnName(columnName);
            List<String> list1=businessFieldSettingMapper.selectBusinessFieldSettingBycolumnNameAndisForeignKey(columnName);
            if (list.size()>0){
                lists.add(list);
            }else if(list1.size()>0){
                lists01.add(list1);
            }
        }
        lists.addAll(lists01);
        return lists;
    }

    @Override
    public List<BusinessFieldSetting> selectisForeignKeyById(Long sysObjId2) {
        return businessFieldSettingMapper.selectisForeignKeyById(sysObjId2);
    }

    @Override
    public FieldSettingVo selectFieldByTableId(Long associationTable) {
        return businessFieldSettingMapper.selectFieldByTableId(associationTable);
    }
}
